import { AfterViewInit, Component, OnInit, Injector, NgZone, ViewChildren, QueryList, Inject, forwardRef } from '@angular/core';
import { NotifyService } from '../../../service/notify.service';
import { SessionService } from '../../../service/session.service';
import { BaseTabComponent } from '../base-tab/base-tab.component';
import { SingleTermComponent } from './single-term/single-term.component';
import { SplitComponent } from 'angular-split';
import { MyTabPanel } from '../../public/tabview/tabview.component';


@Component({
  selector: 'app-term',
  templateUrl: './term.component.html',
  styleUrls: ['./term.component.css']
})
export class TermComponent extends BaseTabComponent implements AfterViewInit {
  counter = 1;
  termlist = [1];
  direction = "horizontal";
  constructor(@Inject(forwardRef(() => MyTabPanel)) tabPanel: MyTabPanel, protected injector: Injector, private ngZone: NgZone) {
    super(tabPanel, injector.get(SessionService), injector.get(NotifyService));
  }
  onTabActive() {
  }
  @ViewChildren(SingleTermComponent) termCompList: QueryList<SingleTermComponent>;
  @ViewChildren('mySplit') splitCompList: QueryList<SplitComponent>;
  reconnect() {
    this.termCompList.forEach(item => {
      item.newTermSession();
    })
  }
  onWindowResize() {
    this.termCompList.forEach(item => {
      item.onWindowResize();
    })
  }
  split() {
    this.counter++;
    this.termlist.push(this.counter);
    setTimeout(() => {
      this.splitCompList.forEach(item => {
        item.dragProgress$.subscribe(x => this.ngZone.run(() => this.onWindowResize()));
      })
      this.onWindowResize();
    })
  }
  toggleDirection() {
    if (this.direction == "horizontal") this.direction = "vertical";
    else this.direction = "horizontal";
    setTimeout(() => this.onWindowResize());
  }
  close(id) {
    for (let i = 0; i < this.termlist.length; i++) {
      if (this.termlist[i] == id) {
        this.termlist.splice(i, 1);
        setTimeout(() => this.onWindowResize());
        break;
      }
    }
  }
  ngAfterViewInit() {
    this.splitCompList.forEach(item => {
      item.dragProgress$.subscribe(x => this.ngZone.run(() => this.onWindowResize()));
    })
  }
  onTermStateChange(msg) {
    this.onStateChange.emit(msg);
  }
}
